#include <iostream>
#include <unordered_map>
#include <gtest/gtest.h>

/* ==================================================================================
 *  测试套件: 可以理解为一个测试环境, 在单元测试之前进行测试环境初始化, 
 *            测试完毕后进行测试环境清理, 可以有多个测试套件
 *  全局测试套件: 在整体的测试中, 只会初始化一次环境, 在所有测试用例完毕后, 才会清理环境
 *               需要自己定义一个套件环境类, 然后继承testing::Environment类, 
 *               分别实现成员函数SetUp和TearDown             
 * ================================================================================== */

class MyEnvironment : public testing::Environment 
{
public:
    virtual void SetUp() override
    {
        std::cout << "Initialization of the environment before unit tests are executed\n";
    }
    virtual void TearDown() override
    {
        std::cout << "Cleanup of the environment after the unit test is executed\n";
    }
};
TEST(MyEnvironment, test1) { std::cout << "unit test1\n"; } 
TEST(MyEnvironment, test2) { std::cout << "unit test2\n"; }

std::unordered_map<std::string, std::string> hashmap;
class MyMapTest : public testing::Environment 
{
public:
    virtual void SetUp() override
    {
        std::cout << "Initialization of the environment before unit tests are executed\n";
        hashmap.emplace("eat", "吃");
        hashmap.emplace("dinner", "晚饭");
    }
    virtual void TearDown() override
    {
        std::cout << "Cleanup of the environment after the unit test is executed\n";
        hashmap.clear();
    }
};
TEST(MyMapTest, test1) 
{ 
    std::cout << "unit test1\n"; 
    EXPECT_EQ(hashmap.size(), 2);
    hashmap.erase("eat");
} 

TEST(MyMapTest, test2) 
{ 
    std::cout << "unit test2\n"; 
    EXPECT_EQ(hashmap.size(), 2);
} 

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MyEnvironment());   // 添加全局测试套件
    testing::AddGlobalTestEnvironment(new MyMapTest());       
    (void)RUN_ALL_TESTS();
    return 0;
}